package bestTeam.WalkieDoggie.client.Composites;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import bestTeam.WalkieDoggie.client.WalkieDoggie;
import bestTeam.WalkieDoggie.client.walkieStatus;
import bestTeam.WalkieDoggie.client.Serializables.UserProfileData;
import bestTeam.WalkieDoggie.client.Serializables.UserRequest;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HistoryComposite extends Composite {

	private ArrayList<UserRequest> requests;
	static HashMap<String, UserProfileData> myWalkersMap;

	public ArrayList<UserRequest> getRequests() {
		return requests;
	}

	private Label title;
	private VerticalPanel ver;
	private StackPanel stackMyHistory;
	private Label emptyLabel;

	public HistoryComposite(HashMap<UserRequest,UserProfileData> requestsMap){



		title = new Label("");
		
		
		ver = new VerticalPanel();
		
		title = new Label("History of Walkies: ");
		title.setStyleName("titleForView");
		
		Label exp = new Label("\t shows past Walkies that you've created or volunteered");
		
		HorizontalPanel hor = new HorizontalPanel();
		hor.add(title);
		
		hor.add(exp);
		hor.setCellVerticalAlignment(exp, HasVerticalAlignment.ALIGN_BOTTOM);
		
		ver.add(hor);
		
		ver.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		ver.setSpacing(5);
		initWidget(ver);
		ver.setSize("100%", "100%");

		stackMyHistory = new StackPanel();
		//ver.add(title);
		ver.setCellWidth(title, "100%");
		ver.setCellHeight(title, "15px");
		ver.setCellHorizontalAlignment(title, HasHorizontalAlignment.ALIGN_LEFT);
		ver.add(stackMyHistory);
		stackMyHistory.setSize("100%", "100%");

		emptyLabel = new Label("no items to show");
		ver.add(emptyLabel);

		if(!WalkieDoggie.loginInfo.isNewUser())
			loadHistry();




	}


	public void loadHistry() {

		if(WalkieDoggie.requestsOwnersMap!=null){
			UserRequest[] tmp = (UserRequest[])WalkieDoggie.requestsOwnersMap.keySet().toArray(new UserRequest[0]);
			requests = getHistoryRequests(tmp);
		}

		String[] walkersIds = getWalkersIds(requests);
		WalkieDoggie.usersService.getUsers(walkersIds, new AsyncCallback<UserProfileData[]>() {

			@Override
			public void onSuccess(UserProfileData[] walkers) {

				myWalkersMap = buildMap(walkers);
				fillWalkies();

			}


			@Override
			public void onFailure(Throwable caught) {
				WalkieDoggie.handleError(caught);

			}
		});

	}



	private HashMap<String, UserProfileData> buildMap(UserProfileData[] walkers) {
		HashMap<String, UserProfileData> map = new HashMap<String, UserProfileData>();

		for (UserProfileData walker : walkers) {
			map.put(walker.getId(), walker);
		}

		return map;
	}

	private ArrayList<UserRequest> getHistoryRequests(UserRequest[] requestsArray) {
		ArrayList<UserRequest> res = new ArrayList<UserRequest>();

		for (UserRequest userRequest : requestsArray) {

			if(userRequest.getCreatedBy().equals(WalkieDoggie.loginInfo.getUserData().getId()) &&
					userRequest.getDate().before(new Date()))
				res.add(userRequest);
		}

		return res;
	}

	private void fillWalkies() {
		boolean f = false;
		for (UserRequest request : requests) {

			displayRequestHisotry(request);

		}
		if(f)
			emptyLabel.setText("");

	}


	private void displayRequestHisotry(UserRequest request) {

		String msg = WalkieDoggie.fmt.format(request.getDate())+"\t"+request.getTimeFrame();
		HistoryRequest historyRequest = new HistoryRequest(request);
		stackMyHistory.add(historyRequest,msg);
		emptyLabel = new Label("");
	}

	private String[] getWalkersIds(ArrayList<UserRequest> requests) {

		ArrayList<String> walkers = new ArrayList<String>();

		for (UserRequest request : requests) {
			if(request.getApprovedWalkerID()!=null)
				walkers.add(request.getApprovedWalkerID());
		}

		return (String[])walkers.toArray(new String[0]);
	}

}

